import { get } from 'lodash';
import { getCurrentUser } from '@/utils/user';

// 得到{fieldName：xxx, title: xxx}
export function getSmartSearchSchema(schema) {
  const resSchema = Object.entries(schema).reduce((res, [key, value]) => {
    res.push({
      ...value,
      fieldName: key,
    });
    return res;
  }, [] as any[]);

  return {
    supportFilterFields: resSchema,
    systemViews: [
      {
        viewId: 'systemView0001',
        name: '默认视图',
        version: '2022-06-22 13:55:59s',
        viewType: 'custom',
        panelConfig: {
          searchFields: resSchema.map(({ fieldName }) => {
            return {
              fieldName,
            };
          }),
        },
      },
    ],
  };
}

// 检验是否为权限人
export function checkIsOwner(row: any, ownerFieldName = 'owner') {
  const currentUser = getCurrentUser();
  const ownerId = get(row, ownerFieldName);
  return ownerId === currentUser.id;
}
